import { Quote } from './../../domain/quote';
import { QuotService } from './../../services/quote.service';
import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from "@angular/forms";


@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
  form: FormGroup;
  quote: Quote = {
    "id": "0",
    "cn": "我突然就觉得自己像个华丽的木偶,演尽了所有的悲欢离合,可是背上总是有无数闪亮的银色丝线,操纵我哪怕一举手一投足。",
    "en": "I suddenly feel myself like a doll,acting all kinds of joys and sorrows.There are lots of shining ",
    "pic": "/assets/image/quotes/0.jpg"
  }
  /**
   * Creates an instance of LoginComponent.
   * @param {FormBuilder} fb  模版表单的简写
   * @param {QuotService} quotService$  $ 代表是一个流
   * @memberof LoginComponent
   */
  constructor(private fb: FormBuilder, private quotService$: QuotService) {
    this.quotService$
    .getQuote()
    .subscribe(quote => {
      this.quote = quote
    })
  }

  ngOnInit() {
    this.form = this.fb.group({
      email: ['', Validators.compose([Validators.required, Validators.email])],
      password: ['', Validators.compose([Validators.required, this.validate])]
    })
  }

  onSubmit(form, ev: Event) {
    ev.preventDefault()
    console.log(form)
    console.log(ev)
  }

  validate(c: FormControl): {[key: string]: any} {
    if (!c.value) {
      return null;
    }
    if (c.value.length > 2) {
      return null;
    }else {
      return {error: '必须大于2位！'}
    }

  }

}
